package com.whmnx.chitchat.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.whmnx.chitchat.R;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.EmptyListUtils;
import com.whmnx.chitchat.view.CustomLoadingDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.whmnx.chitchat.view.CustomLoadingDialog.customProgressDialog;

/**
 * author : huang
 * date   : 2020/7/20 0020 11:23
 * version: 1.0
 * @author Administrator
 */
public abstract class BaseActivity extends SuperBaseActivity {

    private BaseApplication mApplication;
    protected Context mContext;
    public View mView;
    // 布局
    private FrameLayout mFrameLayout;
    // 标题名字
    private TextView head_title;
    public TextView tv_right,tv_left;
    public ImageButton ib_back;
    public CheckBox bt_checkBox;
    private Unbinder bind;
    private static BaseActivity mForegroundActivity;
    private RelativeLayout mRl_head;
    public ImageButton ib_share;
    private CustomLoadingDialog dialog;
    public ViewStub vsEmpty;
    private long exitTime;

    /**
     * Android生命周期回调方法-创建
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main2);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        mApplication = (BaseApplication) getApplication();
        mContext = getApplicationContext();
        mApplication.addActvity(this);
        mForegroundActivity = this;
        //主布局
        mFrameLayout = (FrameLayout) findViewById(R.id.fl_container);
        mRl_head = (RelativeLayout) findViewById(R.id.rl_head);
        head_title = (TextView) super.findViewById(R.id.head_title);
        ib_back = (ImageButton) super.findViewById(R.id.ib_back);
        tv_right = (TextView) super.findViewById(R.id.tv_right);
        tv_left = (TextView) super.findViewById(R.id.tv_left);

        vsEmpty = super.findViewById(R.id.vs_empty);
        exitTime = System.currentTimeMillis();
        initView2();
      //  presenterInit();
        loadData();
    }

    protected void presenterInit() {}


    protected void initView2() {
        loadViewLayout();
    }


    @Override
    public void setContentView(int layoutResID) {
        mView = getLayoutInflater().inflate(layoutResID, null);
        setContentView2(mView);
        bind = ButterKnife.bind(BaseActivity.this, mView);
    }


    public void setContentView2(View view) {
        mFrameLayout.removeAllViews();
        mFrameLayout.addView(view);
    }

    /**
     * 标题
     * @param title
     */
    @Override
    public void setTitle(CharSequence title) {
        head_title.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        head_title.setText(titleId);
    }


    /**
     * 右边
     * @param title
     */

    public void setRringTitle(CharSequence title) {
        tv_right.setText(title);
    }

    public void setLeftTitle(int visibility) {
        tv_left.setVisibility(visibility);
    }

    public void setIbBack(int visibility) {
        ib_back.setVisibility(visibility);
    }


    /**
     * 加载布局
     */
    protected abstract void loadViewLayout();


    /**
     * 向后台请求数据
     */
    protected abstract void loadData();


    // 返回
    public void back(View v) {
        onBackPressed();
    }

    public void onleft(View v) {
        onBackPressed();
    }

    public void showEmpty(boolean isShowEmpty, ViewStub mVsEmpty, int emptyType) {
        if (isShowEmpty) {
            EmptyListUtils.loadEmpty(true, mVsEmpty, emptyType);
        } else {
            if (mVsEmpty != null) {
                mVsEmpty.setVisibility(View.GONE);
            }
        }
    }


    /**
     * 返回键
     *
     * @param visibility
     */
    public void setView(int visibility) {
        ib_back.setVisibility(visibility);
    }


    /**
     * 标题栏
     *
     * @param visibility
     */
    public void setHead_title(int visibility) {
        mRl_head.setVisibility(visibility);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mContext = null;
        mFrameLayout = null;
        mView = null;
        head_title = null;
        mApplication.removeActvity(this);
        mApplication = null;
        mForegroundActivity = null;
        HttpClient.cancelRequest(this);
        if (bind != null) {
            bind.unbind();
            bind = null;
        }

        if (customProgressDialog != null) {
            customProgressDialog.dismiss();
        }
        EventBus.getDefault().unregister(this);
    }

    /**
     * 获取当前处于前台的activity
     */
    public static BaseActivity getForegroundActivity() {
        return mForegroundActivity;
    }
}
